package com.hm.service.aop.log;

import com.hm.service.service.main.TaskLogService;
import com.hm.service.utils.TimeUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class PushHandleCaseJobAspect {
    private static final Logger logger = LoggerFactory.getLogger(PushHandleCaseJobAspect.class);

    private final TaskLogService taskLogService;

    public PushHandleCaseJobAspect(TaskLogService taskLogService) {
        this.taskLogService = taskLogService;
    }

    //定义一个切点,具体方法
    @Pointcut("execution(public * com.hm.service.task.PushHandleCaseJob.execute(..))")
    public void authCut() {

    }

    @Around("authCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        //执行方法开始
        long beginTimestamp = System.currentTimeMillis();
        String beginTime = TimeUtils.getCurrentTime();
        //执行原始方法
        Object proceed = point.proceed();
        //执行方法结束
        long endTimestamp = System.currentTimeMillis();
        String endTime = TimeUtils.getCurrentTime();
        long l = endTimestamp - beginTimestamp;
        taskLogService.addTaskLog("100001", beginTime, endTime, String.valueOf(l));
        logger.info("PushHandleCase环绕切点耗时：{}毫秒", l);
        return proceed;
    }

    @AfterThrowing(value = "authCut()", throwing = "e")
    public void afterThrow(JoinPoint joinPoint, Exception e) {
        logger.info("PushHandleCaseJob异常：{}", e.getMessage());
    }

    @Before("authCut()")
    public void before(JoinPoint joinPoint) {

    }

    @After("authCut()")
    public void after(JoinPoint joinPoint) {

    }


}
